﻿using System.Collections.Generic;
using System.Linq;
using DL.Health.Interface;
using UnityEngine;

namespace DL.Combat.Base.Detection
{
    public delegate void WeaponDetectionCastEvent(Transform target);
    public class WeaponDetectionCast : MonoBehaviour
    {
        [SerializeField]
        private Transform pointHead;
        [SerializeField]
        private Transform pointTail;
        [SerializeField]
        private LayerMask layer;
        [SerializeField]
        private Transform[] Points; //射线发射点
        [SerializeField]
        private GameObject particle; //粒子效果
       
        private RaycastHit[] raycastHits = new RaycastHit[6];
        private List<Transform> HurtedTargets = new();
        private Dictionary<int, Vector3> m_LastPoints = new Dictionary<int, Vector3>(); //存放上个位置信息
        
        [SerializeField]
        protected bool CanAttackCast;
        public WeaponDetectionCastEvent DetectionCastEvent;
        
        public bool DebugRay = true;
        
        private void Start()
        {
            for (int i = 0; i < Points.Length; i++)
            {
                m_LastPoints.Add(Points[i].GetHashCode(), Points[i].position);
            }
        }
        
        private void LateUpdate()
        {
            if (CanAttackCast)
            {
                DetectionCast(Points);
            }
        }
        
        public void AttackCastOpen()
        {
            CanAttackCast = true;
        }
        
        public void AttackCastClose()
        {
            CanAttackCast = false;
            HurtedTargets.Clear();
            firstDarw = true;
        }

        public IDamageable[] GetDamageableObject()
        {
            var damageables = HurtedTargets
                .Where(go => go != null) // 确保对象不为null
                .Select(go => go.GetComponent<IDamageable>()) // 获取IDamageable组件
                .Where(d => d != null) // 确保组件存在
                .ToArray(); // 转换为数组
            return damageables;
        }

        private bool firstDarw;

        private void DetectionCast(Transform[] points)
        {
            for (int i = 0; i < points.Length; i++)
            {
                var nowPos = points[i];
                m_LastPoints.TryGetValue(nowPos.GetHashCode(), out Vector3 lastPos);
                var position = nowPos.position;
                if (DebugRay && !firstDarw)
                {
                    var newA = pointHead.position;
                    var newB = pointTail.position;
                    Debug.DrawRay(lastPos, position - lastPos, Color.blue, 1f);
                    Debug.DrawLine(newA, newB, Color.red, 1f);
                }

                var ray = new Ray(lastPos, position - lastPos);
                var count = Physics.RaycastNonAlloc(ray, raycastHits, Vector3.Distance(lastPos, position), layer,
                    QueryTriggerInteraction.Ignore);
                if (count > 0)
                {
                    for (int j = 0; j < count; j++)
                    {
                        var item = raycastHits[j];
                        if (item.collider == null) continue;
                        if(HurtedTargets.Contains(item.transform)) continue;
                        DetectionCastEvent?.Invoke(item.transform);
                        HurtedTargets.Add(item.transform);
                        if (particle)
                        {
                            var go = Instantiate(particle, item.point, Quaternion.identity);
                            Destroy(go, 3f);
                        }
                    }
                }
                if (nowPos.position != lastPos)
                {
                    m_LastPoints[nowPos.GetHashCode()] = nowPos.position; //存入上个位置信息
                }
            }
            firstDarw = false;
        }
    }
}